package org.globalSearchexample.ws;

import javax.xml.namespace.QName;

import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.apache.cassandra.thrift.Cassandra.system_add_column_family_args;
import org.eventexample.ws.EventsStub;
import org.eventexample.www.events.Event;
import org.eventexample.www.events.EventsArray;
import org.eventexample.www.events.GetAllEvents;
import org.eventexample.www.events.GetAllEventsResponse;
import org.example.www.globalsearch.GlobalEventList;
import org.example.www.globalsearch.GetAllData;
import org.example.www.globalsearch.GetAllDataFault;
import org.example.www.globalsearch.GetAllDataResponse;
import org.example.www.globalsearch.GlobalData;
import org.example.www.globalsearch.GlobalEvents;
import org.example.www.globalsearch.GlobalMovies;
import org.example.www.globalsearch.GlobalMoviesList;
import org.example.www.globalsearch.GlobalNews;
import org.example.www.globalsearch.GlobalNewsList;
import org.example.www.globalsearch.GlobalRestaurant;
import org.example.www.globalsearch.GlobalRestaurantList;
import org.globalSearchexample.ws.GetAllDataFaultException;
import org.globalSearchexample.ws.GlobalSearchSkeletonInterface;
import org.movieexample.ws.MovieStub;
import org.movieexample.www.movie.GetAllMovieData;
import org.movieexample.www.movie.GetAllMovieDataResponse;
import org.movieexample.www.movie.Movie;
import org.newsexample.ws.NewsStub;
import org.newsexample.www.news.GetAllNews;
import org.newsexample.www.news.GetAllNewsResponse;
import org.newsexample.www.news.News;
import org.restaurantexample.ws.RestaurantServiceStub;
import org.sjsusearch.www.restaurantservice.GetAllRestaurantData;
import org.sjsusearch.www.restaurantservice.GetAllRestaurantDataResponse;
import org.sjsusearch.www.restaurantservice.Restaurant;

public class GlobalSearchService implements GlobalSearchSkeletonInterface
{

	private static final String RESTAURANT_SERVICE_END_POINT = "http://localhost:8080/axis2/services/RestaurantService";
	private static final String NEWS_SERVICE_END_POINT = "http://localhost:8080/axis2/services/News";
	private static final String EVENTS_SERVICE_END_POINT = "http://localhost:8080/axis2/services/Events";
	private static final String MOVIES_SERVICE_END_POINT = "http://localhost:8080/axis2/services/Movie";
	
	@Override
	public GetAllDataResponse getAllData(GetAllData getAllData)
			throws GetAllDataFaultException {
		try
		{
			GetAllDataResponse response = new GetAllDataResponse();
			GlobalData data = new GlobalData();
			
			/*RPCServiceClient restaurantServiceClient = new RPCServiceClient();
			Options restaurantOption = restaurantServiceClient.getOptions();
			EndpointReference restaurantRef = new EndpointReference(RESTAURANT_SERVICE_END_POINT);
			restaurantOption.setTo(restaurantRef);
			restaurantOption.setAction("urn:getAllRestaurantData");
			
			QName rName = new QName("http://www.SJSUsearch.org/RestaurantService/", "getAllRestaurantData");
			Object[] opArgs = new Object[]{ };
			Class[] returnTypes = new Class[]{GetAllRestaurantDataResponse.class};
			Object[] rResponse = restaurantServiceClient.invokeBlocking(rName, opArgs, returnTypes);
			Object result =(Object) rResponse[0];
			System.out.println(result.toString());*/
			
			GlobalEventList globalEventData = new GlobalEventList();
			GlobalRestaurantList globalRestaurantData = new GlobalRestaurantList();
			GlobalNewsList globalNewsData = new GlobalNewsList();
			GlobalMoviesList globalMovieData = new GlobalMoviesList();
						
			EventsStub eStub = new EventsStub(EVENTS_SERVICE_END_POINT);
			RestaurantServiceStub rStub = new RestaurantServiceStub(RESTAURANT_SERVICE_END_POINT);
			NewsStub nStub = new NewsStub(NEWS_SERVICE_END_POINT);
			MovieStub mStub = new MovieStub(MOVIES_SERVICE_END_POINT);
			
			//Events Service
			GetAllEvents getAllEvents = new GetAllEvents();
				getAllEvents.setIn("");
				
			GetAllEventsResponse eResponse = null;
			eResponse = eStub.getAllEvents(getAllEvents);
			Event[] eventArray = eResponse.getOut().getEvent();
			
			//Get the data from Event and feed to Global List data
			for(int i= 0;i<eventArray.length;i++)
			{
				GlobalEvents gEvent = new GlobalEvents();
				gEvent.setEvent_Id(eventArray[i].getEvent_id());
				gEvent.setEvent_desctiption(eventArray[i].getEvent_description());
				gEvent.setEvent_date_time(eventArray[i].getEvent_date_time());
				gEvent.setEvent_link(eventArray[i].getEvent_link());
				gEvent.setEvent_start_date(eventArray[i].getEvent_start_date());
				gEvent.setEvent_title(eventArray[i].getEvent_title());
				
				//Add to global array
				globalEventData.addEvents(gEvent);
			}
			
			data.setEventsData(globalEventData);
				
			//Restaurant Service
			GetAllRestaurantData getAllRestaurant = new GetAllRestaurantData();
			getAllRestaurant.setIn("");
			
			GetAllRestaurantDataResponse rResponse = null;
			rResponse = rStub.getAllRestaurantData(getAllRestaurant);
			Restaurant[] restaurantArray = rResponse.getOut().getRestaurantList();
			
			//Get the data from Event and feed to Global List data
			for(int i= 0;i<restaurantArray.length;i++)
			{
				GlobalRestaurant gRestaurant = new GlobalRestaurant();
				gRestaurant.setRestaurant_id(restaurantArray[i].getRestaurant_id());
				gRestaurant.setRestaurant_cusine(restaurantArray[i].getCuisine());
				gRestaurant.setRestaurant_name(restaurantArray[i].getRestaurant_name());
				gRestaurant.setRestaurant_rating(restaurantArray[i].getRestaurant_rating());
				gRestaurant.setRestaurant_lattitude(restaurantArray[i].getRestaurant_latitude());
				gRestaurant.setRestaurant_longitude(restaurantArray[i].getRestaurant_longitude());
				gRestaurant.setRestaurant_location(restaurantArray[i].getRestaurant_location());
				
				//Add to global array
				globalRestaurantData.addRestaurant(gRestaurant);
			}
			
			data.setRestaurantData(globalRestaurantData);
			
			//News Data retrival
			GetAllNews getAllNews = new GetAllNews();
			getAllNews.setIn("");
			
			GetAllNewsResponse nResponse = null;
			nResponse = nStub.getAllNews(getAllNews);
			News[] newsArray = nResponse.getGetAllNewsResponse().getNews();
			
			//Get the data from Event and feed to Global List data
			for(int i= 0;i<newsArray.length;i++)
			{
				GlobalNews gNews = new GlobalNews();
				gNews.setNews_author(newsArray[i].getNews_author());
				gNews.setNews_category(newsArray[i].getNews_category());
				gNews.setNews_description(newsArray[i].getNews_description());
				gNews.setNews_Id(newsArray[i].getNews_id());
				gNews.setNews_link(newsArray[i].getNews_link());
				gNews.setNews_publicationdate(newsArray[i].getNews_publication_date());
				gNews.setNews_title(newsArray[i].getNews_title());
				
				//Add to global array
				globalNewsData.addNews(gNews);
			}
			
			data.setNewsData(globalNewsData);
			
			//Movie Data Retrival
			GetAllMovieData getAllMovie = new GetAllMovieData();
			getAllMovie.setIn("");
			
			GetAllMovieDataResponse mResponse = null;
			mResponse = mStub.getAllMovieData(getAllMovie);
			Movie[] movieArray = mResponse.getOut().getMovies();
			
			//Get the data from Event and feed to Global List data
			for(int i= 0;i<movieArray.length;i++)
			{
				GlobalMovies gMovies = new GlobalMovies();
				gMovies.setMovie_id(movieArray[i].getMovie_id());
				gMovies.setMovie_name(movieArray[i].getMovieName());
				gMovies.setTheatre_name(movieArray[i].getTheatreName());
				gMovies.setMovie_rating(movieArray[i].getMovie_Rating());
				gMovies.setTheatre_lattitude(movieArray[i].getLatitude());
				gMovies.setTheatre_longitude(movieArray[i].getLongitude());
				gMovies.setTheatre_address(movieArray[i].getAddress());
				
				//Add to global array
				globalMovieData.addMovie(gMovies);
			}
			
			data.setMovieData(globalMovieData);
			
			response.setOut(data);
			
			return response;			
		}
		catch(Exception ex)
		{
			GetAllDataFault fault = new GetAllDataFault();
			fault.setGetAllDataFault(ErrorMessage.ServiceUnavailableError);
			
			GetAllDataFaultException exception = new GetAllDataFaultException();
			exception.setFaultMessage(fault);
			throw exception;
		}
	}

}
